package com.twu.biblioteca;

import java.util.ArrayList;

public class BibliotecaApp
{


public static void main(String[] args) {

    String libraryNumber="";

    boolean loggedIn=false;

    boolean shouldContinue=true;
        Library lib = new Library();

        UserInterfaceForApplication ui=new UserInterfaceForApplication();

    Users users=new Users();

    ArrayList<MenuAction> menuItem=new ArrayList<MenuAction>();
    menuItem.add(new ListBooks(lib));
    menuItem.add(new CheckOutBook(lib));
    menuItem.add(new ReturnBook(lib));
    menuItem.add(new ListMovies(lib));
    menuItem.add(new CheckOutMovie(lib));
    menuItem.add(new ReturnMovie(lib));
    menuItem.add(new ListUserInformation(lib));
    menuItem.add(new Quit());
    menuItem.add(new Logout());




    ArrayList<MenuAction> menuItemsInitial=new ArrayList<MenuAction>();
    menuItemsInitial.add(new Login());
    menuItemsInitial.add(new Quit());


    Menu menu=new Menu(ui,menuItem);

    Menu initialMenu=new Menu(ui,menuItemsInitial);


    ui.display("Welcome to Biblioteca");
    boolean appexit=false;

while(appexit==false)
{

    while (loggedIn == false)
    {
        initialMenu.displayMenu();
        try
        {
            int optionChosen = ui.takeInputForOption();
            MenuAction menuAction =initialMenu.getMenuActionToBePerformed(optionChosen);


            if(menuAction!=null)
            {
                Result result=menuAction.performAction(ui,"");
                if(result.successful==true && !result.message.equals("Quitting the Application"))
                {

                    loggedIn = true;
                    libraryNumber=result.message;
                    shouldContinue=true;
                }
                else
                {
                    appexit=true;
                    shouldContinue=false;
                    break;
                }

            }

        }
        catch (Exception e)
        {
            ui.display("Invalid Input!.....please enter again!");
        }
    }




        while (shouldContinue)
        {
            menu.displayMenu();
            try
            {
                int optionChosen = ui.takeInputForOption();


                MenuAction menuAction = menu.getMenuActionToBePerformed(optionChosen);


                if (menuAction != null)
                {

                    Result returnedValue = menuAction.performAction(ui, libraryNumber);
                    if (menuAction.toString().equals("to Logout") && returnedValue.successful == true)
                    {

                        libraryNumber="";
                        shouldContinue = false;
                        loggedIn=false;
                    }

                    if(menuAction.toString().equals("to Quit") && returnedValue.successful==true)
                    {
                            shouldContinue = false;
                            appexit=true;
                    }


                }
            }
            catch (Exception e)
            {
                ui.display("Invalid Input!.....please enter again!");
            }


        }




}


    }


}
